package com.dwxt.sport.business.service;

import com.alibaba.fastjson.JSON;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.VideoSurveillanceMapper;
import com.dwxt.sport.business.entity.VideoSurveillance;
import com.dwxt.sport.business.vo.videoSurveillance.InsertOrUpdateVideoSurveillanceVo;
import com.dwxt.sport.business.vo.videoSurveillance.SelectVideoSurveillanceVo;
import com.dwxt.sport.exception.CustomException;
import com.dwxt.sport.util.BusinessValidationUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 视频监控 Service
 *
 * @author Kuang.JiaZhuo
 * @date 2020-07-27 11:19
 */
@Service
public class VideoSurveillanceService {

    @Resource
    VideoSurveillanceMapper videoSurveillanceMapper;

    /**
     * @方法名：insertVideoSurveillance
     * @描述： 新增视频监控
     * @作者： kjz
     * @日期： Created in 2020/7/27 14:44
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertVideoSurveillance(InsertOrUpdateVideoSurveillanceVo insertVo) {
        VideoSurveillance videoSurveillance = new VideoSurveillance()
                .setId(SnowIdUtil.getInstance().nextId())
                .setCreateTime(LocalDateTime.now())
                .setIsDelete(VariableConstants.STRING_CONSTANT_0)
                .setName(insertVo.getName()) //设备名称
                .setUnitType(insertVo.getEquipmentType()) //设备类型
                .setLimitation(insertVo.getLimitation()) //极限人数
                .setSportsArea(insertVo.getSportsArea()) //运动面积
                .setEquipmentLocation(insertVo.getEquipmentLocation()) //设备位置
                .setWaveCurrentAddress(insertVo.getWaveCurrentAddress()) //波流地址
                .setEquipmentArea(insertVo.getEquipmentArea()) //设备区域
                .setMotionType(insertVo.getMotionType());//运动类型

        return videoSurveillanceMapper.insert(videoSurveillance);
    }

    /**
     * @方法名：updateVideoSurveillance
     * @描述： 更新视频监控
     * @作者： kjz
     * @日期： Created in 2020/7/27 14:54
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateVideoSurveillance(InsertOrUpdateVideoSurveillanceVo updateVo) {
        String id = updateVo.getId();
        if(!BusinessValidationUtil.verifyId(id)){
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        VideoSurveillance videoSurveillance = new VideoSurveillance()
                .setId(id).setUpdateTime(LocalDateTime.now())
                .setName(updateVo.getName()) //设备名称
                .setUnitType(updateVo.getEquipmentType()) //设备类型
                .setLimitation(updateVo.getLimitation()) //极限人数
                .setSportsArea(updateVo.getSportsArea()) //运动面积
                .setEquipmentLocation(updateVo.getEquipmentLocation()) //设备位置
                .setWaveCurrentAddress(updateVo.getWaveCurrentAddress()) //波流地址
                .setEquipmentArea(updateVo.getEquipmentArea()) //设备区域
                .setMotionType(updateVo.getMotionType());//运动类型

        return videoSurveillanceMapper.updateByPrimaryKeySelective(videoSurveillance);
    }

    /**
     * @方法名：getVideoSurveillanceByPage
     * @描述： 按条件获取视频监控列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/7/27 15:00
     */
    public PageInfo<VideoSurveillance> getVideoSurveillanceByPage(SelectVideoSurveillanceVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        System.err.println(JSON.toJSON(selectVo));
        List<VideoSurveillance> list = videoSurveillanceMapper.getVideoSurveillanceByPage(selectVo);
        PageInfo<VideoSurveillance> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteVideoSurveillanceByIds
     * @描述： 根据ids批量删除视频监控
     * @作者： kjz
     * @日期： Created in 2020/7/27 15:12
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteVideoSurveillanceByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }

        String[] idArray = ids.split(",");
        //删除消息和工作人员的关联
        return this.videoSurveillanceMapper.deleteVideoSurveillanceByIds(idArray);
    }

    /**
     * @方法名：getVideoSurveillanceById
     * @描述： 根据id查询视频监控详情
     * @作者： kjz
     * @日期： Created in 2020/7/27 15:29
     */
    public VideoSurveillance getVideoSurveillanceById(String id) {
        if(!BusinessValidationUtil.verifyId(id)){
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        return videoSurveillanceMapper.selectByPrimaryKey(id);
    }
}
